<?php
/**
 * 我的地址
 *
 *
 *
 *
 
 */

// use Shopnc\Tpl;
defined('InShopNC') or exit('Access Invalid!');

class member_addressControl extends mobileMemberControl
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 地址列表
     */
    public function address_listOp()
    {
        $model_address = Model('address');
        $condition['member_id'] = $this->member_info['member_id'];
        $address_list = $model_address->getAddressList($condition);
        output_data(array(
            'address_list' => $address_list
        ));
    }

    /**
     * 默认地址详细信息
     */
    public function address_defaultOp()
    {
        $model_address = Model('address');
        $condition = array();
        $condition['is_default'] = '1';
        $address_info = $model_address->getAddressInfo($condition);
        if ($address_info['member_id'] == $this->member_info['member_id']) {
            output_data(array(
                'address_info' => $address_info
            ));
        } else {
            output_error('地址不存在');
        }
    }

    /**
     * 地址详细信息
     */
    public function address_infoOp()
    {
        $address_id = intval($_POST['address_id']);
        
        $model_address = Model('address');
        
        $condition = array();
        $condition['address_id'] = $address_id;
        $address_info = $model_address->getAddressInfo($condition);
        if (! empty($address_id) && $address_info['member_id'] == $this->member_info['member_id']) {
            output_data(array(
                'address_info' => $address_info,
            ));
        } else {
            output_error('地址不存在');
        }
    }

    /**
     * 删除地址
     */
    public function address_delOp()
    {
        $address_id = intval($_POST['address_id']);
        
        $model_address = Model('address');
        
        $condition = array();
        $condition['address_id'] = $address_id;
        $condition['member_id'] = $this->member_info['member_id'];
        $model_address->delAddress($condition);
        output_data(array(
            'msg' => "删除成功"
        ));
    }

    /**
     * 新增地址
     */
    public function address_addOp()
    {
        $model_address = Model('address');
        
        $address_info = $this->_address_valid();
        
        $result = $model_address->addAddress($address_info);
        if ($result) {
            output_data(array(
                'address_id' => $result
            ));
        } else {
            output_error('保存失败');
        }
    }

    /**
     * 编辑地址
     */
    public function address_editOp()
    {
        $address_id = intval($_POST['address_id']);
        
        $model_address = Model('address');
        
        // 验证地址是否为本人
        $address_info = $model_address->getOneAddress($address_id);
        if ($address_info['member_id'] != $this->member_info['member_id']) {
            output_error('参数错误');
        }
        
        $address_info = $this->_address_valid();
        
        $result = $model_address->editAddress($address_info, array(
            'address_id' => $address_id
        ));
        if ($result) {
            output_data(array(
                'msg' => "保存成功"
            ));
        } else {
            output_error('保存失败');
        }
    }

    /**
     * 验证地址数据
     */
    private function _address_valid()
    {
        $obj_validate = new Validate();
        $obj_validate->validateparam = array(
            array(
                "input" => $_POST["true_name"],
                "require" => "true",
                "message" => '姓名不能为空'
            ),
            array(
                "input" => $_POST["area_info"],
                "require" => "true",
                "message" => '地区不能为空'
            ),
            array(
                "input" => $_POST["address"],
                "require" => "true",
                "message" => '地址不能为空'
            ),
            array(
                "input" => $_POST['tel_phone'] . $_POST['mob_phone'],
                'require' => 'true',
                'message' => '联系方式不能为空'
            )
        );
        $error = $obj_validate->validate();
        if ($error != '') {
            output_error($error);
        }
        
        $data = array();
        $data['member_id'] = $this->member_info['member_id'];
        $data['true_name'] = $_POST['true_name'];
        $data['province_id'] = intval($_POST['province_id']);
        $data['area_id'] = intval($_POST['area_id']);
        $data['city_id'] = intval($_POST['city_id']);
        $data['area_info'] = $_POST['area_info'];
        $data['address'] = $_POST['address'];
        $data['tel_phone'] = $_POST['tel_phone'];
        $data['mob_phone'] = $_POST['mob_phone'];
        $data['is_default'] = $_POST['is_default'];
        return $data;
    }

    /**
     * 地区列表
     */
    public function area_listOp()
    {
        $area_id = intval($_POST['area_id']);
        $model_area = Model('area');
        
        $condition = array();
        if ($area_id > 0) {
            $condition['area_parent_id'] = $area_id;
            // $condition['area_parent_id']=array('lt', 32);
        } else {
            $condition['area_deep'] = 1;
            $condition['area_id'] = array(
                'lt',
                32
            );
        }
        $area_list = $model_area->getAreaList($condition, 'area_id,area_name');
        output_data(array(
            'area_list' => $area_list
        ));
    }

    /**
     * 区县列表
     */
    public function getAreasOp()
    {
        $model_area = Model('area');
        $con['area_deep'] = 1;
        $con['area_id'] = array(
            'lt',
            32
        );
        $area_province = $model_area->getAreaList($con, 'area_id,area_name');
        $province = array();
        $city = array();
        $area = array();
        for($i=0,$length=count($area_province); $i<$length; $i++) {
            array_push($province,$area_province[$i]['area_name']);
            
            $con1['area_deep'] = 2;
            $con1['area_parent_id'] = $area_province[$i]['area_id'];
            $area_city = $model_area->getAreaList($con1, 'area_id,area_name');
            $city_temp = array();
            $area_temp = array();
            for($j=0,$length1=count($area_city); $j<$length1; $j++) {
                array_push($city_temp,$area_city[$j]['area_name']);
            
                $con2['area_deep'] = 3;
                $con2['area_parent_id'] = $area_city[$j]['area_id'];
                $area_area = $model_area->getAreaList($con2, 'area_name');
                $area_temp1 = array();
                foreach ($area_area as $key => $value) {
                    array_push($area_temp1,array_values($value));
                }
                array_push($area_temp,$area_temp1);
            }
            array_push($area,$area_temp);
            array_push($city,$city_temp);
        }
        
        output_data(array(
            'province' => $province,
            'city' => $city,
            'area' => $area
        ));
    }
    
    /**
     * 区县列表
     */
    public function getAreaIdsOp()
    {
        $model_area = Model('area');
        $con['area_deep'] = 1;
        $con['area_id'] = array(
            'lt',
            32
        );
        $area_province = $model_area->getAreaList($con, 'area_id');
        $province = array();
        $city = array();
        $area = array();
        for($i=0,$length=count($area_province); $i<$length; $i++) {
            array_push($province,$area_province[$i]['area_id']);
    
            $con1['area_deep'] = 2;
            $con1['area_parent_id'] = $area_province[$i]['area_id'];
            $area_city = $model_area->getAreaList($con1, 'area_id');
            $city_temp = array();
            $area_temp = array();
            for($j=0,$length1=count($area_city); $j<$length1; $j++) {
                array_push($city_temp,$area_city[$j]['area_id']);
    
                $con2['area_deep'] = 3;
                $con2['area_parent_id'] = $area_city[$j]['area_id'];
                $area_area = $model_area->getAreaList($con2, 'area_id');
                $area_temp1 = array();
                foreach ($area_area as $key => $value) {
                    array_push($area_temp1,array_values($value));
                }
                array_push($area_temp,$area_temp1);
            }
            array_push($area,$area_temp);
            array_push($city,$city_temp);
        }
    
        output_data(array(
            'provinceIds' => $province,
            'cityIds' => $city,
            'areaIds' => $area
        ));
    }
}
